<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: User.php 197 2011-02-18 12:45:33Z beimuaihui $
 */



//class Permission extends Baogg_Db_Table {
class QaFilter extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'qa_filter';
		
		$this->primary= $this->_primary='qa_filter_id';
		parent::__construct();
	}
	
	
	/**
	 * legacy,moved to qa_field
	 * @param unknown_type $filter_id
	 * @param unknown_type $arr_field_map
	 * @param unknown_type $qtypes
	 */
	public function genFilterByFilterID($filter_id,$arr_field_map=array(),$qtypes=array()){
		
		$output='';
		
		$rs_filter=$this->getOne($filter_id);
		$v=$rs_filter[0];

		$Qa_Questions=new Qa_Questions();
		
		$QaField=new QaField();		
		$rs_field=$QaField->getOne(@$rs_filter[0]['qa_field_id']);
		$field_name=$rs_field[0]['qa_field_code'];
		
		
		if(in_array($rs_field[0]['qa_field_code'],array('id','submitdate'))){					
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$rs_filter[0]['operation']).'</td>';
			$output.= "<td><input type='text' name='field_value[$field_name]' value='{$rs_filter[0]['values']}' />  </td>";
			return $output;
		}
		
		$QaReport=new QaReport();
		$rs_report=$QaReport->getOne(@$rs_field[0]['qa_report_id']);
		$Qa_Surveys=new Qa_Surveys();
		$rs_qa_surveys=$Qa_Surveys->getOne($rs_report[0]['survey_id']);
		$language=$rs_qa_surveys[0]['language'];
		
		if(!$arr_field_map){			
			$arr_field_map=$Qa_Questions->genFieldMap(@$rs_report[0]['survey_id']);			
		}
		if(!$qtypes){
			Zend_Loader::loadFile('question_attribute.php','config/qa/',true);
			$qtypes=getqtypelist();
		}
		
		
		$type=$arr_field_map[$field_name]['type'];
		$qid=$arr_field_map[$field_name]['qid'];
		$arr_answer=array();
		$Qa_Answers=new Qa_Answers();
		
		$field_op_selected=$rs_filter[0]['operation'];
		$field_value_text=$rs_filter[0]['values'];
		$arr_field_value_text=explode(",",$field_value_text);
		
		if($qtypes[$type]['answerscales']!=0){
			$scale_id=isset($arr_field_map[$field_name]['scale_id'])?$arr_field_map[$field_name]['scale_id']:0;
			if(!isset($arr_answer[$qid])){				
				$rs_sub_answer=$Qa_Answers->getList(array('qid'=>(int)$qid,'language'=>$language),array('sort'=>'sortorder','dir'=>'asc'));
				foreach($rs_sub_answer as $v_sub_answer){
					$arr_answer[$qid][$v_sub_answer['scale_id']][$v_sub_answer['code']]=$v_sub_answer['answer'];
				}
			}
			//isset($arr_answer[$qid][$val]) && $rs[$k][$field_name]=$arr_answer[$qid][$val];
			
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$rs_filter[0]['operation'],'multi').'</td>';
			$output.= "<td><select name='field_value[{$field_name}][]' multiple='multiple'>";
			foreach((array)$arr_answer[$qid][$scale_id] as $v_code=>$v_answer){
				$str_selected=in_array($v_code,$arr_field_value_text)?'  selected="selected" ':'';
				$output.="<option value=\"{$v_code}\" {$str_selected}>({$v_code})".strip_tags($v_answer)."</option>";
			}
			$output.="</select>  </td>";
			
		}else if(in_array($type,array('G','Y','C','E'))){
			$arr_value=array();
			$arr_value['G']=array('F','M');
			$arr_value['Y']=array('Y','N');
			$arr_value['C']=array('Y','U','N');
			$arr_value['E']=array('I','S','D');
			
			$arr_label=array_map(array('BAOGG_LANGUAGE','get'),$arr_value[$type]);
			$arr_option=array_fill(0,count($arr_value[$type]),'<option value="%s" %s>%s</option>');
			$arr_selected=array_fill(0,count($arr_value[$type]),'');
			$arr_selected_only=	array_intersect($arr_value[$type], $arr_field_value_text);
			($selected_only_cnt=count($arr_selected_only)) && ($arr_selected+=array_combine(array_keys($arr_selected_only),array_fill(0,$selected_only_cnt,' selected="selected" ')));
				
			$arr_option=array_map("sprintf",$arr_option,$arr_value[$type],$arr_selected,$arr_label);
			
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$rs_filter[0]['operation'],'multi').'</td>';
			$output.= "<td><select name=\"field_value[$field_name][]\" multiple='multiple'>".implode("",$arr_option)."</select></td>";
			
		}else if(in_array($type,array('5','B'))){
			$arr_value=array();
			$arr_value['5']=range(1,5);
			$arr_value['B']=range(1,10);			
			
			$arr_label=$arr_value[$type];
			$arr_option=array_fill(0,count($arr_value[$type]),'<option value="%d" %s>%d</option>');
			$arr_selected=array_fill(0,count($arr_value[$type]),'');
			$arr_selected_only=	array_intersect($arr_value[$type], $arr_field_value_text);
			($selected_only_cnt=count($arr_selected_only)) && ($arr_selected+=array_combine(array_keys($arr_selected_only),array_fill(0,$selected_only_cnt,' selected="selected" ')));
				
			$arr_option=array_map("sprintf",$arr_option,$arr_value[$type],$arr_selected,$arr_label);
			
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$rs_filter[0]['operation'],'multi').'</td>';
			$output.= "<td><select name=\"field_value[$field_name][]\" multiple='multiple'>".implode("",$arr_option)."</select></td>";
			
		}else if(in_array($type,array('M','P')) ){
			//TODO			
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$field_op_selected,'check').'</td>';
			
			$rs_qa_questions=$Qa_Questions->getList(array("parent_qid"=>$qid,'title'=>@$arr_field_map[$field_name]['aid']));
			if($rs_qa_questions){				
				$str_checked=$field_value_text=='Y'?'checked="checked"':'';
				$output.='<td><input type="checkbox" name="field_value['.$field_name.']"  '.$str_checked.'  value="Y"  /></td>';
			}else{
				$output.= "<td><input type=\"text\" name=\"field_value[{$field_name}]\" value=\"{$field_value_text}\" value=\"Y\" /></td>";
			}
		}else{
			$output.= '<td>'.$rs_field[0]['qa_field_name'].":</td>";
			$output.='<td>'.Qa_Questions::genOp($field_name,$rs_filter[0]['operation']).'</td>';
			$output.= "<td><input type='text' name='field_value[$field_name]' value='{$field_value_text}' />  </td>";
		}
		
		return $output;
		
		
	}
}